from socket import *
import threading


def send(udp_socket, send_ip, send_port):
    while 1:
        send_data = input("请输入要发送的数据：")
        udp_socket.sendto(send_data.encode("utf-8"), (send_ip, send_port))


def recv(udp_socket):
    while 1:
        recv_data = udp_socket.recvfrom(1024)
        print("\n接收到的数据为:%s" % recv_data[0].decode('utf-8'))


def main():
    udp_socket = socket(AF_INET, SOCK_DGRAM)
    udp_socket.bind(("192.168.14.96", 8888))
    send_ip = '192.168.14.237'
    send_port = 8888
    t_send = threading.Thread(target=send, args=(udp_socket, send_ip, send_port))
    t_recv = threading.Thread(target=recv, args=(udp_socket,))

    t_send.start()
    t_recv.start()


if __name__ == '__main__':
    main()
